package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.SUserDutyDao;
import com.fdb.efp.nls.service.domain.SUserDuty;
import com.fdb.efp.nls.service.facade.SUserDutyService;
import com.fdb.efp.nls.service.vo.SUserDutyVO;
import com.fdb.efp.nls.service.vo.SUserOrgByDutyIdVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>ServiceImpl: 系统用户岗位表				<p>
 * @author chenqm1
 * @date 2018-05-03
 */
@Service("sUserDutyService")
public class SUserDutyServiceImpl implements SUserDutyService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(SUserDutyServiceImpl.class);

	@Autowired
	private SUserDutyDao sUserDutyDao;
	
	/**
	 * 新增操作
	 */
	public int insertSUserDuty(SUserDutyVO inSUserDutyVo) {
		logger.debug("当前新增数据为:"+ inSUserDutyVo.toString());
		int num;
		try {
			SUserDuty sUserDuty = new SUserDuty();
			beanCopy(inSUserDutyVo, sUserDuty);
			sUserDuty.setLoginUserId(sUserDuty.getUserCode());
			setInsertDefaultProperty(sUserDuty);
			num = sUserDutyDao.insertSUserDuty(sUserDuty);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(SUserDutyVO inSUserDutyVo) {
		logger.debug("当前删除数据条件为:"+ inSUserDutyVo);
		int num;
		try {
			SUserDuty sUserDuty = new SUserDuty();
			beanCopy(inSUserDutyVo, sUserDuty);
			num = sUserDutyDao.deleteByPk(sUserDuty);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inSUserDutyVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(SUserDutyVO inSUserDutyVo) {
		logger.debug("当前修改数据为:"+ inSUserDutyVo.toString());
		int num;
		try {
			SUserDuty sUserDuty = new SUserDuty();
			beanCopy(inSUserDutyVo, sUserDuty);
            sUserDuty.setLoginUserId(sUserDuty.getUserCode());
			setUpdateDefaultProperty(sUserDuty);
			num = sUserDutyDao.updateByPk(sUserDuty);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inSUserDutyVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public SUserDutyVO queryByPk(SUserDutyVO inSUserDutyVo) {
		
		logger.debug("当前查询参数信息为:"+ inSUserDutyVo);
		try {
			SUserDuty querySUserDuty = new SUserDuty();
			beanCopy(inSUserDutyVo, querySUserDuty);
			SUserDuty queryRslSUserDuty = sUserDutyDao.queryByPk(querySUserDuty);
			if (Objects.nonNull(queryRslSUserDuty)) {
				SUserDutyVO outSUserDutyVo = beanCopy(queryRslSUserDuty, new SUserDutyVO());
				logger.debug("当前查询结果为:"+ outSUserDutyVo.toString());
				return outSUserDutyVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 根据岗位代码和机构代码查询法人机构代码用户
	 * @param dutyCode
	 * @param orgCode
	 * @return
	 */

	@Override
	public List<SUserOrgByDutyIdVO> queryLegalUserByDutyIdLeagalId(String dutyCode , String orgCode) {
		logger.debug("当前查询参数信息为:"+ dutyCode+"----"+ orgCode);
		try {
			SUserDuty querySUserDuty = new SUserDuty();
			querySUserDuty.setDutyCode(dutyCode);
			List<SUserOrgByDutyIdVO> queryRslSUserDuty = sUserDutyDao.queryLegalUserByDutyIdLeagalId(dutyCode, orgCode);
			if (Objects.nonNull(queryRslSUserDuty)) {
				logger.debug("当前查询结果为:"+ queryRslSUserDuty.toString());
				return queryRslSUserDuty;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

    /**
     * 根据岗位代码和机构代码查询用户
     * @param dutyCode
     * @param orgCode
     * @return
     */
    @Override
    public List<SUserOrgByDutyIdVO> queryOrgUserByDutyIdOrgId(String dutyCode, String orgCode) {
        logger.debug("当前查询参数信息为:"+ dutyCode+"----"+orgCode);
        try {
            SUserDuty querySUserDuty = new SUserDuty();
            querySUserDuty.setDutyCode(dutyCode);
            List<SUserOrgByDutyIdVO> queryRslSUserDuty = sUserDutyDao.queryOrgUserByDutyIdOrgId(dutyCode,orgCode);
            if (Objects.nonNull(queryRslSUserDuty)) {
                logger.debug("当前查询结果为:"+ queryRslSUserDuty.toString());
                return queryRslSUserDuty;
            } else {
                logger.debug("当前查询结果为空!");
            }
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
        }
        return null;
    }

    /**
     * 查询操作
     */
	@SuppressWarnings("unchecked")
    @Override
    public List<SUserDutyVO> queryList(SUserDutyVO inSUserDutyVo) {

        logger.debug("当前查询参数信息为:"+ inSUserDutyVo);
        try {
            SUserDuty querySUserDuty = new SUserDuty();
            beanCopy(inSUserDutyVo, querySUserDuty);
            List<SUserDuty> queryRslSUserDuty = sUserDutyDao.queryList(querySUserDuty);
            if (Objects.nonNull(queryRslSUserDuty)) {
                List<SUserDutyVO> outSUserDutyVo = (List<SUserDutyVO>) beansCopy(queryRslSUserDuty, SUserDutyVO.class);
                logger.debug("当前查询结果为:"+ outSUserDutyVo.toString());
                return outSUserDutyVo;
            } else {
                logger.debug("当前查询结果为空!");
            }
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
        }
        return null;
    }


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SUserDutyVO> queryAllOwner(SUserDutyVO sUserDutyVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		SUserDuty sUserDuty = beanCopy(sUserDutyVo, new SUserDuty());
		List<SUserDutyVO> list = null;
		try {
			List<SUserDuty> sUserDutys = sUserDutyDao.queryAllOwnerByPage(sUserDuty);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ sUserDutys.size());
			pageSet(sUserDutys, sUserDuty);
			list = (List<SUserDutyVO>) beansCopy(sUserDutys, SUserDutyVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	@Override
	public List<SUserOrgByDutyIdVO> queryUsersByDuty(SUserDutyVO inVo) {
		logger.debug("当前查询参数信息为:"+ inVo.getDutyCode());
		try {
			List<SUserOrgByDutyIdVO> queryRslSUserDuty = sUserDutyDao.queryUsersByDuty(inVo.getDutyCode());
			if (Objects.nonNull(queryRslSUserDuty)) {
				logger.debug("当前查询结果为:"+ queryRslSUserDuty.toString());
				return queryRslSUserDuty;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}
}
